#include <algorithm>
#include <iostream>
#include <vector>

/**
 * NowCoder KY 134: Maximum Reimbursement Limit
 */

std::vector<double> bills;

double minRem;

void search(int idx, double limit) {
    // Skip if limit is exceeded
    if (limit < 0) return;

    // Update minimum remaining value
    if (idx == bills.size()) {
        minRem = std::min(minRem, limit);
        return;
    }

    // Search following bills
    search(idx + 1, limit - bills[idx]);
    search(idx + 1, limit);
}

int main() {
    double limit;
    int nBills;
    while (scanf("%lf %d", &limit, &nBills) != EOF) {
        // Read bills
        if (nBills == 0) break;
        bills.clear();
        for (int i = 0; i < nBills; i++) {
            int nItems;
            scanf("%d", &nItems);
            bool valid = true;
            double total = 0;
            for (int j = 0; j < nItems; j++) {
                char kind;
                double value;
                scanf(" %c:%lf", &kind, &value);
                if (kind < 'A' || kind > 'C' || value > 600)
                    valid = false;  // read remaining items even this bill is
                                    // invalid
                total += value;
            }
            if (!valid || total > 1000) continue;
            bills.push_back(total);
        }

        // Depth-first search possible bills
        minRem = UINT32_MAX;
        search(0, limit);
        printf("%.2lf\n", limit - minRem);
    }
}